//
//  WebViewController.m
//  MY QQapp
//
//  Created by apple on 2016/11/21.
//  Copyright © 2016年 liuchengming. All rights reserved.
//

#import "WebViewController.h"

@interface WebViewController () <UIWebViewDelegate,UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *web;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *av;

@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UISearchBar * searchbar= [UISearchBar new];
    searchbar.delegate = self;
    self.navigationItem.titleView = searchbar ;
    
    searchbar.returnKeyType = UIReturnKeySearch ;
    NSURL *url=[NSURL URLWithString:@"http://www.baidu.com/"];
    NSURLRequest * req = [NSURLRequest requestWithURL:url];
    self.web.delegate = self;
    [self.web loadRequest:req];
    [self.av startAnimating];
}

- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    NSLog(@"");
    NSString * str = searchBar.text;
    if (![str hasPrefix:@"http://"])
    {
        str = [NSString stringWithFormat:@"http://%@",str];
    }
    NSURL * url =  [NSURL URLWithString:str];
    NSURLRequest * req = [NSURLRequest requestWithURL:url];
    
    [self.web loadRequest:req];
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"开始加载");
}

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    [self.av stopAnimating];
    NSLog(@"加载完成");
}

- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [self.av stopAnimating];
    NSLog(@"%@",error);
}

- (void) viewWillAppear:(BOOL)animated
{
    self.navigationController.toolbarHidden = NO;
}
-(void)viewDidDisappear:(BOOL)animated
{
    self.navigationController.toolbarHidden = YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
